package com.gjszn.kh.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gjszn.kh.context.SysContext;
import com.gjszn.kh.model.AdminPO;
import com.gjszn.kh.model.RecommendationCode;
import com.gjszn.kh.service.RecommendationCodeSerivce;
import com.gjszn.kh.utils.Base64Util;
import com.gjszn.kh.utils.CheckUtil;
import com.gjszn.kh.utils.DateUtil;


/**    
* @Title: UserController.java  
* @Package com.gjszn.kh.controller.admin    
* @author jys@nx-yt.com    
* @date 2016年1月5日 下午3:46:10  
* @version V1.0    
* @Description: TODO(用一句话描述该文件做什么)
********************************************/
@Controller
@RequestMapping("/admin/code")
public class CodeController {
	private static Logger log = LogManager.getRootLogger();
	
	@Autowired
	private RecommendationCodeSerivce recommendationCodeSerivce;
	
	@RequestMapping("")
	public String toCodeView(){
		return "admin/code";
	}
	
	@RequestMapping(value="load", method=RequestMethod.POST)
	public @ResponseBody JSONObject load(int page, int rows, HttpSession session){
		JSONObject jsonObject = new JSONObject();
		AdminPO admin = (AdminPO)session.getAttribute(SysContext.SYS_ADMIN);
		if(admin == null){
			jsonObject.put("total", 0);
			jsonObject.put("rows", null);
			return jsonObject;
		}
		log.info("查询会员单位信息，从第" + page + "页，之后的" + rows + "行数据");
		List<RecommendationCode> recommendationCodes = null;
		int total = 0;
		if(!"admin".equals(admin.getNickname())){
			recommendationCodes = recommendationCodeSerivce.listForPage(admin.getNickname(), page, rows);
			total = recommendationCodeSerivce.total(admin.getNickname());
		}else{
			recommendationCodes = recommendationCodeSerivce.listForPage(page, rows);
			total = recommendationCodeSerivce.total(null);
		}
		jsonObject.put("total", total);
		jsonObject.put("rows", recommendationCodes);
		return jsonObject;
	}
	
	@RequestMapping(value="query", method=RequestMethod.POST)
	public @ResponseBody JSONObject query(String code, HttpSession session){
		if(CheckUtil.isEmpty(code)){
			return null;
		}
		log.info("q_code:" + code);
		AdminPO admin = (AdminPO)session.getAttribute(SysContext.SYS_ADMIN);
		RecommendationCode recommendationCode = null;
		if("admin".equals(admin.getNickname())){
			recommendationCode = recommendationCodeSerivce.load(null, code);
		}else{
			recommendationCode = recommendationCodeSerivce.load(admin.getNickname(), code);
		}
		JSONObject resultJson = new JSONObject();
		JSONArray rows = new JSONArray();
		rows.add(recommendationCode);
		resultJson.put("total", recommendationCode == null ? 0 : 1);
		resultJson.put("rows", rows);
		log.info(JSON.toJSONString(recommendationCode));
		return resultJson;
	}
	
	@RequestMapping(value="add", method=RequestMethod.POST)
	public @ResponseBody JSONObject add(String name, String member, String code, HttpServletRequest request){
		log.info("add:member" + member);
		JSONObject jsonObject = new JSONObject();
		if(CheckUtil.isEmpty(name, member, code)){
			jsonObject.put("status", "0");
			jsonObject.put("msg", "数据不能为空");
			return jsonObject;
		}
		
		String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath()+ "/selected"
				+"/" + Base64Util.encode(member) + "?code="+ Base64Util.encode(code);
		log.info("生成的url：" + url);
		RecommendationCode recommendationCode = new RecommendationCode(name, member + "" + code, member, DateUtil.getNowDateTime(), url);
		if(recommendationCodeSerivce.add(recommendationCode)){
			jsonObject.put("status", "1");
			jsonObject.put("msg", "处理成功");
			return jsonObject;
		}
		log.info("开始处理addOrUpdate完成");
		jsonObject.put("status", "-2");
		jsonObject.put("msg", "处理失败");
		return jsonObject;
	}
	
	@RequestMapping(value="update", method=RequestMethod.POST)
	public @ResponseBody JSONObject update(int id, String name, String member, String code, HttpServletRequest request){
		log.info("开始处理addOrUpdate:" + id);
		JSONObject jsonObject = new JSONObject();
		if(CheckUtil.isEmpty(name, member, code)){
			jsonObject.put("status", "0");
			jsonObject.put("msg", "数据不能为空");
			return jsonObject;
		}
		
		String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+ request.getContextPath()+ "/selected"
				+"/" + member + "?code="+code;
		log.info("生成的url：" + url);
		RecommendationCode recommendationCode = new RecommendationCode(name, member + "" + code, member, DateUtil.getNowDateTime(), url);
		recommendationCode.setId(id);
		if(recommendationCodeSerivce.update(recommendationCode)){
			jsonObject.put("status", "1");
			jsonObject.put("msg", "处理成功");
			return jsonObject;
		}
		log.info("开始处理addOrUpdate完成");
		jsonObject.put("status", "-2");
		jsonObject.put("msg", "处理失败");
		return jsonObject;
	}
	
	@RequestMapping(value="delete", method=RequestMethod.POST)
	public @ResponseBody JSONObject delete(int id){
		JSONObject jsonObject = new JSONObject();
		if(recommendationCodeSerivce.delete(id) && id >= 1){
			jsonObject.put("status", "1");
			jsonObject.put("msg", "处理成功");
			return jsonObject;
		}
		jsonObject.put("status", "-1");
		jsonObject.put("msg", "处理失败");
		return jsonObject;
		
	}
	
}

